 module solveVSL
      use precisions
      use types_generic
      use routines_specific
    contains

    subroutine vfi(grids, params, tech, valpol)
        implicit none
        
        !! Argument
        type(typeGrids), intent(in) :: grids
        type(typeParams), intent(in)  :: params      
        type(typeValPol), intent(out) :: valpol
        type(typeTech), intent(in) :: tech

        ! For program
        type(typeInfoForMax) :: infoForMax
        real (kind=rk) :: cstar, valstar, optPol(6), beqVal
 
        type(typeForOptRout) :: ForOptRoute
        integer :: ixT, ixA, ixY, ixTPlus1, ixY1, ixARisky, ixPC
        !integer :: maxRisky, maxPCIx

        !! Development
        !real (kind=rk) :: scratchreal
        !integer :: scratchinteger
        
        integer ::  OMP_GET_THREAD_NUM  
        !! Set terminal value
        valpol%val%vOldNoAccess(:, :, :, grids%maxT+1) = 0
        

 
        
        
        
    do ixT = grids%maxT, tech%retireAge, -1
            

            do ixY = 1, grids%maxYO, 1
!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(TID, ixA, infoForMax, optPol, valstar, cstar)
                do ixA = 1, grids%maxA, 1
                       infoForMax%ixA = ixA
                       infoForMax%today = ixT
                       infoForMax%tomorrow = ixT + 1
                       infoForMax%ixY = ixY 
                           infoForMax%annY = grids%YOld(ixY, ixT) 
                       

      
                           
                       infoForMax%Y = infoForMax%annY  
                       infoForMax%solveT_SimF = .true.
                       infoForMax%cashOnHand = grids%A(ixA, ixT)  + infoForMax%Y + tech%SSlevel
                    
                        if (infoForMax%cashOnHand .lt. 0.0_rk) then
                            write(*,*) 'Cash on hand is negative'
                            stop
                        end if

                    if ((tech%ageAtSurvey + infoForMax%today -2).gt.110) then
                         valstar = 0.0_rk
                         cstar = -999.0_rk
                         optPol(5) =  -999.0_rk
                    else                         
                        call getMax(infoForMax, grids, valpol, params, tech, optPol, valstar)
                    end if
                    
                        cstar = optPol(1)
                        valpol%val%vOldNoAccess(ixA, 1, ixY, ixT) = valstar 
                                              
                   
                    if (printAllStatesGlob) write(*,*) 'Passed', ixT, ixY, ixA

                end do !ixA
!$OMP END PARALLEL DO                   
                   
            end do !ixY
 


            if ((printYearUpdatesGlob).and.((ixT.gt.1).or.(ixT.lt.2))) write(*,*) 'Passed Year', ixT, valpol%val%vOldNoAccess(1, 1, 1, ixT) ! inprog
                               
    end do ! ixT
    

    
        do ixT = (tech%retireAge-1), 1, -1



            do ixY = 1, grids%maxYY, 1
                    
                    ixTPlus1 = ixT + 1

!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(TID, ixA,  infoForMax, optPol, valstar,  cstar, scratch, scratch1)
                do ixA = 1, grids%maxA, 1

                    infoForMax%today = ixT
                    infoForMax%tomorrow = ixT + 1
                    infoForMax%ixY = ixY                      
                    infoForMax%annY = 0.0_rk
                    infoForMax%Y =  infoForMax%annY
                    infoForMax%solveT_SimF = .true.
                    infoForMax%cashOnHand = grids%A(ixA, ixT)  + infoForMax%Y
                    infoForMax%ixA = ixA
                
                    if (infoForMax%cashOnHand .lt. 0.0_rk) then
                        write(*,*) 'Cash on hand is negative'
                        stop
                    end if
                                            
                    if ((tech%ageAtSurvey + infoForMax%today -2).gt.110) then
                         valstar = 0.0_rk
                         cstar = -999.0_rk
                         optPol(5) =  -999.0_rk                         
                    else 
                        call getMax(infoForMax, grids, valpol, params, tech, optPol, valstar)
                    end if
                    
                    cstar = optPol(1)
                    valpol%val%vYoungNoAccess(ixA, 1, ixY, ixT) = valstar   
              
                end do !ixA
!$OMP END PARALLEL DO 
 
            end do !ixY


                            
        if ((printYearUpdatesGlob).and.((ixT.gt.80).or.(ixT.lt.80))) write(*,*) 'Passed Year', ixT, valpol%val%vYoungNoAccess(1, 1, 1, ixT) ! inprog
        end do ! ixT
 
        
        
    end subroutine vfi


    
end module solveVSL
   